import { get, del, uploadFile } from '@/utils/request'

export default {
  // 单文件上传到本地 - 使用封装的uploadFile
  async uploadSingle(file) {
    try {
      const result = await uploadFile('/api/upload/single', {
        file,
        name: 'image',
        formData: {}
      });
      return result;
    } catch (error) {
      console.error('单文件上传失败:', error);
      return {
        success: false,
        message: error.message || '上传失败'
      };
    }
  },
  
  // 单文件上传到OSS - 使用封装的uploadFile
  async uploadToOSS(file) {
    try {
      const result = await uploadFile('/api/upload/oss/single', {
        file,
        name: 'image',
        formData: {}
      });
      return result;
    } catch (error) {
      console.error('OSS单文件上传失败:', error);
      return {
        success: false,
        message: error.message || '上传失败'
      };
    }
  },
  
  // 多文件上传到本地 - 使用封装的uploadFile
  async uploadMultiple(files) {
    try {
      const uploadPromises = files.map(file => {
        return uploadFile('/api/upload/multiple', {
          file,
          name: 'images',
          formData: {
            // 可以添加额外的表单数据
          }
        });
      });

      // 等待所有文件上传完成
      const results = await Promise.all(uploadPromises);
      
      // 合并所有上传结果
      const uploadedFiles = results.flatMap(result => 
        result.success && result.data ? result.data : []
      );

      return {
        success: true,
        data: uploadedFiles,
        message: `成功上传 ${uploadedFiles.length} 张图片`
      };
    } catch (error) {
      console.error('多文件上传失败:', error);
      return {
        success: false,
        message: error.message || '上传失败'
      };
    }
  },
  
  // 多文件上传到OSS - 使用封装的uploadFile
  async uploadMultipleToOSS(files) {
    try {
      const uploadPromises = files.map(file => {
        return uploadFile('/api/upload/oss/multiple', {
          file,
          name: 'images',
          formData: {}
        });
      });

      // 等待所有文件上传完成
      const results = await Promise.all(uploadPromises);
      
      // 合并所有上传结果
      const uploadedFiles = results.flatMap(result => 
        result.success && result.data ? result.data : []
      );

      return {
        success: true,
        data: uploadedFiles,
        message: `成功上传 ${uploadedFiles.length} 张图片到OSS`
      };
    } catch (error) {
      console.error('多文件OSS上传失败:', error);
      return {
        success: false,
        message: error.message || '上传失败'
      };
    }
  },
  
  // 删除本地图片
  async deleteLocalImage(filename) {
    const res = await del('/api/upload/images/' + filename)
    return res.data
  },
  
  // 删除OSS图片
  async deleteOSSImage(filename) {
    const res = await del('/api/upload/oss/images/' + filename)
    return res.data
  },
  
  // 检查OSS状态
  async checkOSSStatus() {
    const res = await get('/api/upload/oss/status')
    return res.data
  }
}